// govSuperviseTourist/pages/superviseIndex/superviseIndex.js
const WXAPI = require('../../../api/govSuperviseTourist')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '预付式消费云监管服务平台', // 导航栏 中间的标题
      textColor: '#2C3241', // 标题颜色
      bgColor: '#FFFFFF', // 导航栏背景颜色
      isColor: true,
    },
    pageSize: 4,
    pageNum: 1,
    total: 0,
    changeIndex: 0,
    publicityLs: [], //商家列表
    statusLs: [{
        name: '商家对接公示'
      },
      {
        name: '红榜商家'
      },
      {
        name: '黑榜商家'
      },
    ],
    tipLs: [
      '银行存管，降风险  ', '制式合约，避陷阱', '商家审核，资质全', '消费信息，笔笔清'
    ],
    moreShow: true,
    wxtype: '',
    choiceIndex: 0,
    propShow: false, //弹窗展示
    areaCode: '', //城市区编码
    addressList: [],
    city: '海口市', //
    areaId: '', //区域id
    address: '', //定位地址
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.openType == 1) {
      wx.navigateTo({
        url: '/govSuperviseTourist/pages/redBlackList/red_black_list'
      })
    }
    this.getGovAreaList()
  },
  choiceAddress(e) { //切换服务区域
    var item = e.currentTarget.dataset.item
    var addressList = []
    addressList = this.data.addressList.map((res) => {
      if (res.id == item.id) {
        res.selected = true
      } else {
        res.selected = false
      }
      return res
    })
    this.setData({
      city:item.name,
      areaId:item.id,
      addressList:addressList
    })
    this.getAreaId()
    this.propShowFalse()
  },
  propShowFalse(){
    this.setData({
      propShow:false
    })
  },
  propShowTrue(){
    this.setData({
      propShow:true
    })
  },
  async getGovAreaList(areaCode) { //查询区域列表
    var data = { areaCode: areaCode }
    WXAPI.govAreaList(data,res=>{
      this.setData({
        addressList:res.result,
        propShow:true,
        city:res.result[0].name,
        [`addressList[0].selected`]:true,
      })
      this.getAreaId()
    })
    

    // sessionStorage.setItem('isRequest', '1')
  },
  getAreaId() { //获取areaId
    // console.log(this.addressList)
    let list = this.data.addressList
    list.forEach((item, index) => {
      if (item.selected) {
        this.setData({
          areaId:item.id
        })
      }
    })
    wx.setStorageSync('areaId', this.data.areaId);
    if (this.data.changeIndex == 0) {
       this.getlist()
    } else {
      this.getRedBlackList(this.data.changeIndex)
    }
  },
  changeStatus: function (e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    this.setData({
      changeIndex:index
    })
    this.setData({
      pageNum:1
    })
    if (index == 0) {
      this.getlist()
    } else {
      this.getRedBlackList(index)
    }
  },
  watchMore() { //查看更多
    if (this.data.changeIndex == 0) {
      wx.navigateTo({
        url: '/govSuperviseTourist/pages/sincerityLs/sincerityLs'
      })
    } else {
      var type = 1
      if (this.data.changeIndex == 1) {
        type = 2
      }
      wx.navigateTo({
        url: `/govSuperviseTourist/pages/redBlackList/redBlackList?type=${type}`
      })
    }
  },
  async getlist() { //诚信商家列表
    var data =  {
      areaId: this.data.areaId,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    WXAPI.publicityV2(data,res=>{
       this.setData({
        publicityLs:res.result.records
       })
    })
  },
  lookImg: function (val) {
    let arrnew = this.publicityLs.map(obj => {
      return this.imgHttp + obj.image;
    })
    console.log(arrnew)
    const urls = arrnew
    const current = val
    uni.previewImage({
      urls,
      current
    })
  },
  //红黑榜列表
  async getRedBlackList(e) {
    // 2 红榜  1 黑榜
    var type = 1
    if (e == 1) {
      type = 2
    }
    var data =  {
      areaId: this.data.areaId,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      type: type
    }
    WXAPI.blacklist(data,res=>{
      this.setData({
        publicityLs:res.result.records
      })
    })
  },
})